var isEdit = Number(getParamsByUrl(location.href,'isEdit'));
console.log(isEdit);
$(function(){
	//isEdit为1时是编辑收货地址，0时是添加收货地址
	if(isEdit){
		//获取本地存储的数据
		if(localStorage.getItem("editArInof")){
			var address = JSON.parse(localStorage.getItem("editArInof"));
			console.log(address);
			var html=template('inofTemplate',{result:address});
			$("#showTemplate").html(html);
		}
	}else{
		var html=template('inofTemplate',{result:[]});
		$("#showTemplate").html(html);
	}
	var picker = new mui.PopPicker({layer:3});
	//setData() 支持数据格式为: 数组,,cityData是city.js里的数据
	picker.setData(cityData);
	//展示数据
	$("#showPicker").on('tap',function(){
		picker.show(function(SelectedItem) {
		    var text = SelectedItem[0].text + SelectedItem[1].text+SelectedItem[2].text;
		    $("#showPicker").val(text);
		})		
	});
	
	
	
	//点击按钮，发送ajax请求
	$("#addBtn").on('tap',function(){
		var username = $("[name='username']").val();
		var postcode = $("[name='postcode']").val();
		var address1 = $("[name='address']").val();
		var addressDetail = $("[name='addressDetail']").val();
		var data = {
		address:address1,
		addressDetail:addressDetail,
		recipients:username,
		postcode:postcode
		}
		//判断为添加还是修改
		if(isEdit){
			var url = '/address/updateAddress';
			//往data里面添加数据
			data.id=address.id;
		}else{
			var url = '/address/addAddress';
		}
		$.ajax({
			url:url,
			type:'post',
			data:data,
			success:function(res){
				if(res.success){
					if(isEdit){
						mui.toast("编辑成功");
						setTimeout(function(){
							location.href="address.html";
						},1000);	
					}else{
						mui.toast("添加成功");
						setTimeout(function(){
							location.href="address.html";
						},1000);	
					}
				}
			}
		});
	});
	
	

});
	//获取路径中的关键字方法
	function getParamsByUrl(url,name){
		var params = url.substr(url.indexOf('?')+1);
	//	console.log(params);
	//	var param = params.split('&');将字符串分割成数组
		var param = params.split('=');
		console.log(param);
		if(param[0]==name){
			return param[1];
		}	
		//如果是多个&参数的话，就可以for循环出来
	//	for(var i = 0;i<params.length;i++){
	//		var current = params[i].split('=');		
	//		if(current[0]==name){
	//			console.log(current[1]);
	//		}
	//	}
	};



